package ru.ifmo.croak.impl.storage.team1;

import ru.ifmo.croak.api.MessageListener;
import ru.ifmo.croak.api.QueryRequest;

/**
 * @author Alex Vikharev
 *         21.05.11
 */
public class ListenerContainer {

    private final QueryRequest queryRequest;
    private final MessageListener listener;
    private final Pattern pattern;

    public ListenerContainer(final QueryRequest queryRequest, final MessageListener listener) {
        this.queryRequest = queryRequest;
        this.listener = listener;
        this.pattern = new Pattern(queryRequest.getText());
    }

    public void processMessage(TokenizedMessage message) {
        if ((queryRequest.getAuthor() == null || queryRequest.getAuthor().equals(message.getAuthor())) &&
                pattern.matches(message.getTokens())) {
            listener.messageAvailable(message);
        }
    }
}
